#include "gui.h"
#include "../logic/logic.h"

//-----------Grid-------------------
/*Function: Grid constructor
  set default parametes to field and create Cell matrix*/
Grid :: Grid()
{
    doShowLines = DefDoShowLines;
    x_num = DefaultSizeX;
    y_num = DefaultSizeY;
    playerColor.push_back(Qt::red); 
    playerColor.push_back(Qt::blue); 
    playerColor.push_back(Qt::green); 
    //create Cell matrix
    //allocate memory for cells
    //int max = qMax ( y_num, x_num);
    cells = new Cell * [y_num];
    for ( int i = 0; i < x_num; i++ )
        cells[i] = new Cell [x_num];

    /*TEST*/

    for (int i = 0; i < x_num; i++)
        for (int j = 0; j < y_num; j++){
            static int randInt; //random integer
            static int minRandPrice = 5, maxRandPrice = 300;
            static int minRandInc = 10, maxRandInc = 80;
            randInt = rand() % (maxRandPrice - minRandPrice) + minRandPrice;
            cells[j][i].setPrice(randInt);
            randInt = rand() % (maxRandInc - minRandInc) + minRandInc;
            sprintf ( tmp, "building %d%d", j+1, i+1);
            cells[j][i].setName(tmp);
            cells[j][i].setProductionPrice(randInt);
            cells[j][i].setIncome();
            cells[j][i].setType(mainSession->defaultType);
        }
    
    //set position for every cell;

    for (int i = 0; i < y_num; i++)
        for (int j = 0; j < x_num; j++)
            cells[i][j].setPosition ( j, i);

    //set field geometry
    setMinimumSize ( DefaultSize, DefaultSize );
    prevActCell = getCell (0,0);
    curActCell = getCell (0,0);
};

/*Function: Grid destructor*/
Grid :: ~Grid()
{
};

/*Function: paintEvent
  this functions called when anything forces window to redraw
  here we print field*/
void Grid :: paintEvent ( QPaintEvent *event)
{
    //this is painter area.
    QPainter painter( this);
        
    //calculate size of single cell
    x_size = width()/x_num;
    y_size = height()/y_num;

    //draw lines
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(QPen(Qt::gray, 1));

    if(doShowLines){
        for (int i = 0; i <= width(); ++i)
            painter.drawLine(x_size * i, 0, x_size * i, width());
   
        for (int i = 0; i <= height(); ++i)
            painter.drawLine( 0, y_size * i,  width(), y_size * i);
    }
    //draw cells content
    redrawCells( &painter);
};

/*Function: redrawCells
  this functions called when anything forces window to redraw
  here we force every cell to paint its content to field*/
void Grid :: redrawCells( QPainter *painter)
{ 
    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->setPen(QPen(Qt::black, 1));
    for (int i = 0; i < y_num; i++)
        for (int j = 0; j < x_num; j++)
            cells[i][j].drawCell ( this, painter);        
};

/*Function: mousePressEvent
  this functions called when user clics on the grid
  it causes access to cell*/
void Grid :: mousePressEvent ( QMouseEvent * mevent)
{
    if ( mevent->button() == Qt::LeftButton){
        accessCell ( mevent->pos(), true);
    }
    else if (mevent->button() == Qt::RightButton){
        accessCell ( mevent->pos(), false);
    }

}

/*Function: accessCell
  this functions called when user clics on the known cell*/
void Grid :: accessCell ( QPoint pos, bool opaque)
{
    int i = pos.x() / x_size;
    int j = pos.y() / y_size;
    Cell * cp = getCell ( i, j);
    if ( opaque == true){ //left button
        //change state
        if ( !cp->activeState()){
            prevActCell->unsetActive();           
            cp->setActive();
            prevActCell = cp;
        }
            
        curActCell = cp;        
    }
    else {
        if (cp->getOwner() != NoPlayer)
            return;
		if( cp->getPrice() <= mainSession->getCurrentPlayer()->getMoney()){
			mainSession->getCurrentPlayer()->addBuilding( (Building *) cp);
			mainSession->getCurrentPlayer()->addMoney( -(cp->getPrice()));
		}
		else
			mainWindow->updateStatusBar( "	doesn't have enough money to buy this building");
			mainSession->getCurrentPlayer()->evaluateIncome();
        mainWindow->redrawTurnInfo();
    }
    mainWindow->repaint();
    mainWindow->redrawObjectState();
    mainWindow->redrawPlayersBuilding();
}
